package pl.taskman.testing;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.event.MouseInputListener;

public class CustomButton extends JButton
{
	/**
	 *	Generated serial ID 
	 */
	private static final long	serialVersionUID	= 4135415330640190302L;
	public static boolean		mouseOver			= false;
	public static boolean		mousePressed		= false;
	
	private CustomButtonMouseListener cbml			= null;
	
	public CustomButton()
	{
		super();
		setBorderPainted(false);
		
		if (cbml == null) cbml = new CustomButtonMouseListener();
		
		addMouseListener(cbml);
		addMouseMotionListener(cbml);
	}
	
	@Override
	public void paintComponent(Graphics g)
	{	
		Graphics2D g2 = (Graphics2D) g;
		GradientPaint gradient = null;
		
		if (mouseOver)
			gradient = new GradientPaint((getWidth() / 2), 0, new Color(20, 20, 205, 205), (getWidth() / 2), (int)(getHeight() / 0.8), Color.BLACK, false);
		if (mousePressed)
			gradient = new GradientPaint((getWidth() / 2), 0, Color.BLACK, (getWidth() / 2), getHeight(), Color.BLACK, false);
		if (!mouseOver && !mousePressed)
			gradient = new GradientPaint((getWidth() / 2), 0, Color.DARK_GRAY, (getWidth() / 2), (int)(getHeight() / 0.8), Color.BLACK, false);
		
		g2.setPaint(gradient);
		g2.fillRoundRect(1, 1, super.getWidth(), super.getHeight(), 40, 30);
		
		int fontSize = (int)Math.floor((getHeight() / 3));
		
		g2.setFont(new Font("Lucida Grande", Font.BOLD, fontSize));
		FontMetrics metrics = g2.getFontMetrics();
		Rectangle2D rect = metrics.getStringBounds(getText(), g2);
		
		double rectWidth = rect.getWidth();
		double rectHeight = rect.getHeight();
		
		int posX = (int)((getWidth() / 2) - (rectWidth / 2));
		int poxY = (int)((getHeight() / 2) + (rectHeight / 2) - 3);
		
		g2.setColor(Color.WHITE);
		g2.drawString(getText(), posX, poxY);
	}
}

class CustomButtonMouseListener implements MouseInputListener
{

	@Override
	public void mouseClicked(MouseEvent e)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		CustomButton.mousePressed = true;
		repaintComponents();
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		CustomButton.mousePressed = false;
		repaintComponents();
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		CustomButton.mouseOver = true;
		repaintComponents();
	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		CustomButton.mouseOver = false;
		repaintComponents();
	}

	@Override
	public void mouseDragged(MouseEvent e)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseMoved(MouseEvent e)
	{
		// TODO Auto-generated method stub
	}
	
	public void repaintComponents()
	{
		// TODO Add repaint code
	}
}
